<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="800px" @open="handleDialogOpen()"
    v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="questionModel" :rules="dialogProps.action === 'add' ? addQuestion : editQuestion"
      ref="questionForm" label-width="80px" label-position="right" size="small">
      <el-tabs type="border-card" v-model="activeTabName">
        <el-tab-pane name="exerciseInfoTab" label="基本信息" class="exercise-info-tab-pane">
          <el-row>
            <el-col :span="12">
              <el-form-item label="题目名称" prop="topicName">
                <el-input ref="topicName" :maxlength="50" v-model="questionModel.topicName" autofocus></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="题目类型" prop="topicType">
                <el-select class="parent-select" v-model="questionModel.topicType" placeholder="请选择">
                  <el-option v-for="item in optionTY" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="适用性别" prop="applyGender">
                <el-select class="parent-select" v-model="questionModel.applyGender" placeholder="请选择">
                  <el-option v-for="item in optionSex" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否必填" prop="whether">
                <el-select class="parent-select" v-model="questionModel.whether" placeholder="请选择">
                  <el-option v-for="item in optionWT" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('questionForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { saveQuestion, updateQuestion } from '@/api/Lixueseng520/questionBank'

  export default {
    name: 'question-edit',
    data() {
      return {
        loading: true,
        optionTY: [
          { value: '单选题', label: '单选题' },
          { value: '多选题', label: '多选题' },
        ],
        optionSex: [
          { value: '不限', label: '不限' },
          { value: '男', label: '男' },
          { value: '女', label: '女' }
        ],
        optionWT: [
          { value: '是', label: '是' },
          { value: '否', label: '否' },
        ],
        questionModel: {
          id: 0,
          topicName: '',
          topicType: '',
          applyGender: '',
          whether: '',
        },
        dialogProps: {
          visible: false,
          action: '',
          title: ''
        },
        activeTabName: 'exerciseInfoTab',
        addQuestion: {
          topicName: [
            { required: true, message: '请输入题目名称', trigger: 'blur' },
            { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
          ],
          topicType:[
            { required: true, message: '请选择', trigger: 'change' }
          ],
          applyGender: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          whether:[
             { required: true, message: '请选择', trigger: 'change' }
          ]
        },
        editQuestion: {
          topicName: [
            { required: true, message: '请输入项目名称', trigger: 'blur' },
            { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.dialogProps.action === 'add') {
              this.doSaveQuestion()
            } else {
              this.doUpdateQuestion()
            }
          } else {
            return false
          }
        })
      },
      doUpdateQuestion() {
        this.loading = true
        updateQuestion(this.questionModel.id, this.questionModel)
          .then(response => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.loading = false
            this.dialogProps.visible = false
            this.$emit('save-finished')
          })
          .catch(error => {
            this.loading = false
            this.outputError(error)
          })
      },
      doSaveQuestion() {
        this.loading = true
        saveQuestion(this.questionModel)
          .then(response => {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.loading = false
            this.dialogProps.visible = false
            this.$emit('save-finished')
          })
          .catch(error => {
            this.outputError(error)
          })
      },

      handleDialogClose() {
        this.dialogProps.visible = false
      },
      handleDialogOpen() {
        this.$nextTick(() => {
          this.$refs['questionForm'].clearValidate()
          this.$refs['topicName'].focus()
        })
      },
      outputError(error) {
        console.log(error.response ? error.response : error.message)
        this.loading = false
        this.$message({
          showClose: true,
          message: '出错了，请按F12查看浏览器日志。',
          type: 'error'
        })
      }
    },
    mounted: function () {
      this.$nextTick(() => {
        this.$on('openEditUserDialog', function (exercise) {
          this.questionModel = exercise
          this.dialogProps.action = 'edit'
          this.dialogProps.title = '编辑'
          this.activeTabName = 'exerciseInfoTab'
          this.dialogProps.visible = true
        })
        this.$on('openAddUserDialog', function () {
          this.questionModel = {
            roleIds: []
          }
          this.dialogProps.action = 'add'
          this.dialogProps.title = '新增'
          this.activeTabName = 'exerciseInfoTab'
          this.dialogProps.visible = true
        })
      })
    }
  }
</script>

<style lang="scss" scoped>
  .exercise-info-tab-pane {
    margin-top: 10px;
  }

  .parent-select {
    width: 230px;
  }

  .properties-textarea {
    width: 496px;
  }
</style>
